/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.EnumerationLiteral;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.figures.LabelFigure;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagram.classdiagram.figures.EnumerationLiteralFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * 
 * 
 * @author Chokri Mraidha
 * Enumeration Literal element Edit Part.
 */
public class EnumerationLiteralEditPart extends GraphNodeWithSemanticEditPart {
	
//	/**
//	 * Manager for editing enumeration literal name.
//	 */
//	protected DirectEditManager manager;

	
	/**
	 * 
	 * 
	 * @param modelManager 
	 * @param graphNode 
	 */
	public EnumerationLiteralEditPart(GraphNode graphNode,ModelManager modelManager) {
		super(graphNode,modelManager);
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createEditPolicies()
	 */
	/**
	 * 
	 */
	@Override
	protected void createEditPolicies(){
		super.createEditPolicies();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected IFigure createFigure() {
		return new EnumerationLiteralFigure();
	}
	

	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public EnumerationLiteral getUmlElement() {
		return (EnumerationLiteral)((Uml1SemanticModelBridge)(getGraphNode()).getSemanticModel()).getElement();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
	 */
	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);
		
		EnumerationLiteralFigure f = (EnumerationLiteralFigure)getFigure();
		
		//==>
		/** stereotypes handling */

		/** Stereotype presentation */
		String stereotypePresentation = null;
		if(getGraphNode().getSemanticModel() != null) {
			if(getGraphNode().getSemanticModel().getPresentation() != null) {
				stereotypePresentation = getGraphNode().getSemanticModel().getPresentation();
			} else {
				getGraphNode().getSemanticModel().setPresentation(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION);
				stereotypePresentation = ModelerHelper.TEXT_STEREOTYPE_PRESENTATION;
			}
		}

		// Retrieve current enumeration literal and figure
		org.eclipse.uml2.uml.EnumerationLiteral currentLiteral = getUmlElement();
		com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(getUmlElement());

		IFigure fig = getFigure();
		((EnumerationLiteralFigure) fig).refreshColors(getGraphNode());
		((LabelFigure) fig).refreshPresentation(getPresentation(), currentLiteral, getGraphNode());
		boolean hasIcon  = currentElement.hasIcon();
		boolean hasShape = currentElement.hasShape();
		
		String stereotypes;
		stereotypes = horizontalStereotypesToDisplay();
		f.refreshStereotypes(stereotypes, getPresentation(), hasIcon, hasShape);

		if((stereotypes!="")&& 
				((stereotypePresentation != null) && (stereotypePresentation.equals(ModelerHelper.TEXT_ICON_STEREOTYPE_PRESENTATION) || 
						stereotypePresentation.equals(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION)))){
			//stereotypes=stereotypes.substring(0, stereotypes.length()-2);
			f.setStereotypes(stereotypes);//add stereotypes on the operation figure
		}
		else {
			f.setStereotypes(null);
		}


		//<==
		
		String name = getUmlElement().getName();
		f.setName(name);//set the name
		f.setEnumerationLiteral();
		((GraphicalEditPart) getParent()).setLayoutConstraint(
				this,
				f,
				r);
	}
	
//	/**
//	 * TODO RS: verify this is deprecated.
//	 * 
//	 * @return 
//	 * 
//	 * @deprecated
//	 */
//	@Deprecated
//	protected EnumerationLiteralFigure getEnumerationLiteralFigure(){
//		return (EnumerationLiteralFigure)createFigure() ;
//	}
	
//	/**
//	 * Init the label direct edit manager.
//	 * This method should be called when a Direct Edit request is performed.
//	 */
//	protected void initLabelEditManager() {
//		if(manager==null) {
//			// locator for the cell editor (on the name label)
//			CellEditorLocator locator = new SimpleCellEditorLocator(getFigure());
//
//			// defining how properties are modified in the model
//			PropertyAccessor accessor = new PropertyAccessor() {
//				public String getPropertyName() {
//					return "Name";
//				}
//				public String getValue() {
//					return ((org.eclipse.uml2.uml.NamedElement) getUmlElement()).getName();
//				}
//				public void setValue(String value) {
//					((org.eclipse.uml2.uml.NamedElement) getUmlElement()).setName(value);  
//				}
//			};
//			// defines the controler: link between model and view
//			FigureControler labelControler = new FigureControler(getFigure(), accessor);
//
//			// creates the editing manager.
//			manager = new LabelDirectEditManager(this, locator, labelControler);
//		}
//	}
//	
//	
//	/**
//	 * the direct edit mechanism requires the edit part to handle the direct edit
//	 * request.
//	 * 
//	 * @param req the request that called the direct edit.
//	 */
//	public void performDirectEdit(Request req) {
//		initLabelEditManager();
//		manager.show();
//	}
//
//	/* (non-Javadoc)
//	 * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
//	 */
//	/**
//	 * 
//	 * 
//	 * @param request 
//	 */
//	@Override
//	public void performRequest(Request request){
//		if (RequestConstants.REQ_DIRECT_EDIT.equals(request.getType())) {
//			performDirectEdit(request);
//		}
//	}

	
}
